# vim:ft=dockerfile

# specify architecture.
FROM fedora:41
ARG ARCH=x86_64
ARG MINGW_PREFIX=mingw64

WORKDIR /root
RUN dnf install -y gcc make ${MINGW_PREFIX}-gcc ${MINGW_PREFIX}-gcc-c++ ${MINGW_PREFIX}-winpthreads
RUN dnf install -y p7zip p7zip-plugins curl

ARG VIM_VERSION=v7.2
RUN curl -L -o ./${VIM_VERSION}.tar.gz https://github.com/vim/vim/archive/${VIM_VERSION}.tar.gz
RUN tar xf ${VIM_VERSION}.tar.gz

RUN mkdir -p /out/vim

# windres tweak
RUN printf '%s\n%s\n' '#!/bin/sh' 'exec /usr/bin/'$ARCH'-w64-mingw32-windres "$@"' > /usr/local/bin/windres && chmod +x /usr/local/bin/windres

# `echo $ARCH | tr _ -`: x86_64 -> x86-64.
RUN cd vim-*/src && \
        sed -i -E 's/^CROSS=.+/CROSS=yes/' Make_cyg_ming.mak && \
        sed -i -E 's/^CSCOPE=.+/CSCOPE=no/' Make_cyg_ming.mak && \
        sed -i -E 's/^NETBEANS=.+/NETBEANS=no/' Make_cyg_ming.mak && \
        sed -i -E 's/^GETTEXT=.+/GETTEXT=no/' Make_cyg_ming.mak && \
    { if [ "$ARCH" = i686 ]; then \
        cp /tmp/vim.ico ../runtime/bitmaps/ && \
        cp /tmp/vim.ico ./ && \
        sed -i '1 i#define GetFinalPathNameByHandleW(a, b, c, d) 0' os_mswin.c && \
        sed -i '1 i#define GetConsoleScreenBufferInfoEx(a, b) ;' os_win32.c && \
        sed -i '1 i#define SetConsoleScreenBufferInfoEx(a, b) ;' os_win32.c && \
        sed -i '1 i#define MOUSE_HWHEELED 0x8  /* only defined for NT6.0+ */' os_win32.c && \
        printf '%s\n%s\n%s\n%s\n' '#ifdef _WIN32' 'int is_cygpty(int fd) { return 0; }' 'int is_cygpty_used(void) { return 0; }' '#endif' > iscygpty.c && \
        sed -i '1 i#define wcscpy_s(a, b, c) ;' os_mswin.c && \
        sed -i -E 's/^.*-lstdc.*/LIB += -static-libstdc++ -static-libgcc/' Make_cyg_ming.mak; \
    fi; } && \
        env VIMDLL=yes WINVER=$(if [ "$ARCH" = i686 ]; then echo 0x0501; else echo 0x0601; fi) ARCH=$(echo $ARCH | tr _ -) CROSS_COMPILE=$ARCH-w64-mingw32- STATIC_STDCPLUS=yes STATIC_WINPTHREAD=yes \
        make -f Make_ming.mak gvim.exe vim.exe tee/tee.exe xxd/xxd.exe vimrun.exe && \
        mv vim*.dll gvim.exe vim.exe tee/tee.exe xxd/xxd.exe vimrun.exe /out/vim/

RUN cp -r vim-*/runtime /out/vim

RUN cd /out && 7z a vim-${VIM_VERSION}.7z vim

RUN echo 'run `docker run --rm -i NAME-OF-THIS-IMAGE cat /out/vim-${VIM_VERSION}.7z > OUTPUT-FILENAME.7z` to get result file.'
